package com.chat.common.chat.service.impl;

import com.chat.common.chat.dao.*;
import com.chat.common.chat.domain.entity.*;
import com.chat.common.chat.domain.enums.GroupRoleEnum;
import com.chat.common.chat.domain.enums.RoomTypeEnum;
import com.chat.common.common.utils.AssertUtil;
import com.chat.common.user.domain.entity.User;
import com.chat.common.user.domain.enums.NormalOrNoEnum;
import com.chat.common.chat.service.IRoomService;
import com.chat.common.chat.service.adapter.ChatAdapter;
import com.chat.common.user.service.cache.UserCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RoomServiceImpl implements IRoomService {
    @Resource
    private RoomFriendDao roomFriendDao;
    @Resource
    private RoomDao roomDao;
    @Resource
    private GroupMemberDao groupMemberDao;
    @Autowired
    private UserCache userCache;
    @Autowired
    private RoomGroupDao roomGroupDao;


    @Override
    public RoomFriend createFriendRoom(List<Long> uidList) {
        AssertUtil.isNotEmpty(uidList, "房间创建失败，好友数量不对");
        AssertUtil.equal(uidList.size(), 2, "房间创建失败，好友数量不对");
        String key = ChatAdapter.generateRoomKey(uidList);
        //先判断key 是否存在 如果存在直接恢复
        RoomFriend roomFriend = roomFriendDao.getByKey(key);
        if(Objects.nonNull(roomFriend)) {
            //两个人已经存在了 房间 直接将房间状态更改为可用
            restoreRoomIfNeed(roomFriend);
        }else {
            //新建房间
            Room room = createRoom(RoomTypeEnum.FRIEND);
            roomFriend = createFriendRoom(room.getId(), uidList);
            roomFriendDao.save(roomFriend);
        }

        return roomFriend;
    }

    @Override
    public void disableFriendRoom(List<Long> uidList) {
        AssertUtil.isNotEmpty(uidList, "房间创建失败，好友数量不对");
        AssertUtil.equal(uidList.size(), 2, "房间创建失败，好友数量不对");
        String key = ChatAdapter.generateRoomKey(uidList);
        roomFriendDao.disableFriendRoom(key);
    }



    @Override
    public RoomFriend getFriendRoom(Long uid, Long friendUid) {
        String key = ChatAdapter.generateRoomKey(Arrays.asList(uid, friendUid));
        return roomFriendDao.getByKey(key);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoomGroup createGroupRoom(Long uid) {
        GroupMember selfGroup = groupMemberDao.getSelfGroup(uid);
        AssertUtil.isEmpty(selfGroup,"每个人只能创建一个群");
        Room room = createRoom(RoomTypeEnum.GROUP);
        User userInfo = userCache.getUserInfo(uid);
        //插入群
        RoomGroup roomGroup = ChatAdapter.buildGroupRoom(userInfo, room.getId());
        roomGroupDao.save(roomGroup);
        //插入群主
        GroupMember insert = GroupMember.builder()
                .groupId(roomGroup.getId())
                .uid(uid)
                .role(GroupRoleEnum.LEADER.getType())
                .build();
        groupMemberDao.save(insert);
        return roomGroup;
    }


    private RoomFriend createFriendRoom(Long roomId, List<Long> uidList) {
        return ChatAdapter.buildFriendRoom(roomId, uidList);
    }

    private Room createRoom(RoomTypeEnum roomTypeEnum) {
        Room insert = ChatAdapter.buildRoom(roomTypeEnum);
        roomDao.save(insert);
        return insert;
    }


    private void restoreRoomIfNeed(RoomFriend isRoom) {
        if(isRoom.getStatus().equals(NormalOrNoEnum.NOT_NORMAL.getStatus())){
            roomFriendDao.restoreRoom(isRoom.getId());
        }
    }


}
